package com.yanwq.sayhi.controller.club.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.yanwq.sayhi.R;
import com.yanwq.sayhi.base.BaseActivity;
import com.yanwq.sayhi.base.BaseToolbarActivity;
import com.yanwq.sayhi.common.client.MImageLoader;
import com.yanwq.sayhi.common.client.OkJsonObjectResponseCallback;
import com.yanwq.sayhi.common.client.OkRequestParams;
import com.yanwq.sayhi.common.client.OkRestClient;
import com.yanwq.sayhi.common.event.ClubEvent;
import com.yanwq.sayhi.common.tool.DateFormatTool;
import com.yanwq.sayhi.controller.user.UserMgr;
import com.yanwq.sayhi.dao.bean.Club;
import com.yanwq.sayhi.dao.bean.ClubVip;
import com.yanwq.sayhi.widget.InputDialog;
import com.yanwq.sayhi.widget.MToast;

import butterknife.BindView;
import de.greenrobot.event.EventBus;

public class ClubActivity extends BaseToolbarActivity {
    @BindView(R.id.club_avatar)
    ImageView mIVAvatar;
    @BindView(R.id.club_name)
    TextView mTVName;
    @BindView(R.id.club_time)
    TextView mTVTime;
    @BindView(R.id.club_motto)
    TextView mTVMotto;
    @BindView(R.id.club_apply)
    Button mBApply;

    private Club mClub;
    private int mType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club);
    }

    @Override
    public void init() {
        mClub = (Club) getIntent().getSerializableExtra("club");
        mType = getIntent().getIntExtra("type", 0);
    }

    @Override
    public void initView() {
        refreshView();

        if (mType == 1) {
            mBApply.setText("退出该俱乐部");
        }

        mBApply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mType == 0) {
                    if (mClub.getPermission() == 1) {
                        final InputDialog dialog = new InputDialog(ClubActivity.this);
                        dialog.setPrompt("请输入俱乐部口令");
                        dialog.setOk("加入");
                        dialog.setCancel("算了");
                        dialog.setOkListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mClub.setPassword(dialog.getContent());
                                apply();
                            }
                        });
                        dialog.show();
                    } else {
                        apply();
                    }
                } else {
                    exit();
                }
            }
        });
    }

    private void refreshView() {
        MImageLoader.displayImage(mClub.getPhotoMediumUrl(), mIVAvatar);
        mTVName.setText(mClub.getName());
        mTVTime.setText(String.format("创建于 %s", DateFormatTool.format(mClub.getCreateTime())));
        mTVMotto.setText(mClub.getMotto());
    }

    private void apply() {
        ClubVip clubVip = new ClubVip();
        clubVip.setUser(UserMgr.get());
        clubVip.setClub(mClub);
        clubVip.setName(UserMgr.get().getNickname());
        OkRestClient.getInstance().post(this, "/clubVip/apply", clubVip, new OkJsonObjectResponseCallback() {
            @Override
            public void onStart() {
                showProgressDialog("正在申请");
            }

            @Override
            public void onSuccess(JSONObject jsonObject) {
                MToast.showShort(ClubActivity.this, "已成为会员");
                mBApply.setText("退出俱乐部");
                mType = 1;
                EventBus.getDefault().post(new ClubEvent(ClubEvent.CREATED));
            }

            @Override
            public void onStop() {
                cancelProgressDialog();
            }
        });
    }

    private void exit() {
        OkRequestParams params = new OkRequestParams();
        params.put("userId", UserMgr.get().getId());
        params.put("clubId", mClub.getId());
        OkRestClient.getInstance().post(this, "/clubVip/remove", params, new OkJsonObjectResponseCallback() {
            @Override
            public void onStart() {
                showProgressDialog("正在退出");
            }

            @Override
            public void onSuccess(JSONObject jsonObject) {
                MToast.showShort(ClubActivity.this, "已退出俱乐部");
                mBApply.setText("申请加入");
                mType = 0;
                EventBus.getDefault().post(new ClubEvent(ClubEvent.EXITED));
            }

            @Override
            public void onStop() {
                cancelProgressDialog();
            }
        });
    }
}
